package com.example.common.app;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import java.util.List;

import butterknife.ButterKnife;

/**
 * @author:0.2℃
 * @date： 2021-12-17 00:27
 * @Description ：
 */
public abstract class Activity extends AppCompatActivity {
    private static final int REQUEST_PERMISSION = 0;

    @Override
    protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 在界面未初始化之前调用的初始化窗口
        initWindows();

        if (initArgs(getIntent().getExtras())) {
            // 获得界面的id之后并设置到Activity界面中
            int layoutId = getContentLayoutId();
            setContentView(layoutId);
//            checkPermission();
            initWidget();
            initData();
        } else {
            finish();
        }
    }

//    public void checkPermission() {
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//
//            int hasWritePermission = checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
//            int hasReadPermission = checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE);
//
//
//            List<String> permissions = new ArrayList<String>();
//            if (hasWritePermission != PackageManager.PERMISSION_GRANTED) {
//                permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
//            } else {
//                //    preferencesUtility.setString("storage", "true");
//            }
//
//            if (hasReadPermission != PackageManager.PERMISSION_GRANTED) {
//                permissions.add(Manifest.permission.READ_EXTERNAL_STORAGE);
//
//            } else {
//                //    preferencesUtility.setString("storage", "true");
//            }
//
//            if (!permissions.isEmpty()) {
//                //    requestPermissions(permissions.toArray(new String[permissions.size()]), REQUEST_CODE_SOME_FEATURES_PERMISSIONS);
//                ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_PHONE_STATE},
//                        REQUEST_PERMISSION);
//            }
//        }
//    }
//
//    @Override
//    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
//        switch (requestCode) {
//            case REQUEST_PERMISSION: {
//                for (int i = 0; i < permissions.length; i++) {
//                    if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
//
//
//                        System.out.println("Permissions --> " + "Permission Granted: " + permissions[i]);
//                    } else if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
//                        System.out.println("Permissions --> " + "Permission Denied: " + permissions[i]);
//                    }
//                }
//            }
//            break;
//            default: {
//                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
//            }
//        }
//    }

    /**
     * 初始化我们一些窗口的数据
     */
    protected void initWindows() {

    }

    /**
     * 参数是否初始化成功
     */
    protected boolean initArgs(Bundle bundle) {
        return true;
    }

    /**
     * 得到当前的资源文件id
     * @return 资源文件ID
     */
    protected abstract int getContentLayoutId();

    /**
     * 初始化控件
     */
    protected void initWidget() {
        ButterKnife.bind(this);
    }

    /**
     * 初始化数据
     */
    protected void initData() {

    }

    /**
     * 点击导航页面的返回方法
     * @return
     */
    @Override
    public boolean onNavigateUp() {
        // 当点击导航界面的返回键是
        finish();
        return super.onNavigateUp();
    }

    /**
     * 手机硬件的返回键
     */
    @Override
    public void onBackPressed() {
        // 得到當前的Activity下所有的fragment
        List<androidx.fragment.app.Fragment> fragments = getSupportFragmentManager().getFragments();
        // 判断是否为空
        if (fragments != null && fragments.size() > 0) {
            for (androidx.fragment.app.Fragment fragment : fragments) {
                // 判断是否为我们自己处理的Fragment类型
                if (fragment instanceof com.example.common.app.Fragment) {
                    // 判断是否拦截了返回按钮
                    if (((com.example.common.app.Fragment) fragment).onBackPress()) {
                        return;
                    }
                }
            }
        }

        super.onBackPressed();
        finish();
    }


}
